<?php /* TASKS $Id: vw_log_update.php,v 1.30.4.7 2006/07/27 19:16:08 nybod Exp $ */
global $AppUI, $task_id, $obj, $percent, $can_edit_time_information, $dPconfig, $users;

$perms =& $AppUI->acl();

// check permissions
$canEdit = $perms->checkModuleItem( 'task_log', 'edit', $task_id );
$canAdd = $perms->checkModuleItem( 'task_log', 'add', $task_id );

$task_log_id = intval( dPgetParam( $_GET, 'task_log_id', 0 ) );
$log = new CTaskLog();
if ($task_log_id) {
	if (! $canEdit) {
		$AppUI->setMsg('Access denied', UI_MSG_ERROR);
		$AppUI->redirect();
	}
	$log->load( $task_log_id );
} else {
	if (! $canAdd) {
		$AppUI->setMsg('Access denied', UI_MSG_ERROR);
		$AppUI->redirect();
	}
	$log->task_log_task = $task_id;
	$log->task_log_name = $obj->task_name;
}

// Check that the user is at least assigned to a task
$task = new CTask;
$task->load($task_id);
if (! $task->canAccess($AppUI->user_id)) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$proj = &new CProject();
$proj->load($obj->task_project);

// Task Update Form
$df = $AppUI->getPref( 'SHDATEFORMAT' );
$log_date = new CDate( $log->task_log_date );
$creator = $log->task_log_id ? $log->task_log_creator : $AppUI->user_id;
?>

<!-- TIMER RELATED SCRIPTS -->
<script language="JavaScript">
	// please keep these lines on when you copy the source
	// made by: Nicolas - http://www.javascript-page.com
	// adapted by: Juan Carlos Gonzalez jcgonz@users.sourceforge.net

	var timerID       = 0;
	var tStart        = null;
    var total_minutes = -1;

	function UpdateTimer() {
	   if (timerID) {
	      clearTimeout(timerID);
	      timerID = 0;
	   }

       // One second has passed
       total_minutes = total_minutes + 1;
       var d = new Date(), h, m, s;
       d.setHours(0);
       d.setMinutes(0);
       d.setSeconds(total_minutes);
       h = d.getHours();
       m = d.getMinutes();
       s = d.getSeconds();
       if (10 > h) h = '0' + h;
       if (10 > m) m = '0' + m;
       if (10 > s) s = '0' + s;

	   document.editFrm.task_log_hours.value = h + ':' + m + ':' + s;
	   timerID = setTimeout("UpdateTimer()", 1000);
	}

	function timerStart() {
		if (!timerID) { // this means that it needs to be started
			timerSet();
			document.editFrm.timerStartStopButton.value = "<?php echo $AppUI->_('Stop');?>";
            UpdateTimer();
		} else { // timer must be stoped
			document.editFrm.timerStartStopButton.value = "<?php echo $AppUI->_('Start');?>";
			timerStop();
		}
	}

	function timerStop() {
	   if (timerID) {
	      clearTimeout(timerID);
	      timerID = 0;
          total_minutes = total_minutes / 60;
          document.editFrm.task_log_hours.value = Math.round( (total_minutes / 60) * 100) / 100;
	   }
	}

	function timerReset() {
		document.editFrm.task_log_hours.value = "0.00";
        total_minutes = -1;
	}

	function timerSet() {
		total_minutes = Math.round(document.editFrm.task_log_hours.value * 60) -1;
	}

	function setLogCreator(sel) {
		var f = document.editFrm;
		f.task_log_creator.value = sel.options[sel.selectedIndex].value;
	}

	Calendar.enabled = true;
</script>
<!-- END OF TIMER RELATED SCRIPTS -->

<a name="log"></a>
<form name="editFrm" action="?m=tasks&a=view&task_id=<?=$task_id?>" method="post"
	onsubmit='updateEmailContacts();'>
	<input type="hidden" name="uniqueid" value="<?=uniqid("")?>" />
	<input type="hidden" name="dosql" value="do_updatetask" />
	<input type="hidden" name="task_log_id" value="<?=$log->task_log_id?>" />
	<input type="hidden" name="task_log_task" value="<?=$log->task_log_task?>" />
	<input type="hidden" name="task_log_creator" value="<?=$creator?>" />
	<input type="hidden" name="task_log_name" value="Update :<?=$log->task_log_name?>" />
<table cellspacing="0" cellpadding="3" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<input type="button" class="button" value="<?php echo $AppUI->_('Save');?>" onclick="updateTask()" />
	</td>
<?
if ($AppUI->user_id == $proj->getManager()) {
	$members = array();
	foreach ($users as $u) {
		$members[$u['user_id']] = $u['contact_name'];
	}
?>
	<td>&nbsp;</td>
	<td nowrap="nowrap"><?=$AppUI->_('log for')?></td>
	<td width="100%"><?=arraySelect($members, 'task_members', 'onchange="setLogCreator(this)"', $creator)?></td>
<?
}
?>
</tr>
</table>
<table cellspacing="0" cellpadding="3" border="0" width="100%" class="subinfopanel">
<tr>
<td width='40%' valign='top' align='center'>
	<table width='100%'>
	<tr>
		<td align="right">
			<?php echo $AppUI->_('Date');?>
		</td>
		<td nowrap="nowrap" align="left">
			<input id="idDateLog" type="hidden" name="task_log_date" value="<?php echo $log_date->format( FMT_DATETIME_MYSQL );?>">
			<input id="idDateLogD" type="text" name="log_date" value="<?php echo $log_date->format( $df );?>" class="text" disabled="true" style="width: 64px">
			<img id="idDateLogB" src="images/calendar.gif" title="<?php echo $AppUI->_('Calendar');?>" class="ico"/>
		</td>
		<td align="right" nowrap="nowrap">&nbsp;<?=$AppUI->_('Hours')?></td>
		<td align="left" nowrap="nowrap">
		<?
			$hwpd = floatval($dPconfig['daily_working_hours']);
			if ($hwpd == 0.0) $hwpd = 8.0;
			showSpinBox('task_log_hours', $log->task_log_hours ? $log->task_log_hours : 0, 0, $hwpd, '', 0.5);
		?>
		</td>
		<td align="right">&nbsp;<?=$AppUI->_('Completion')?></td>
		<td align="left">
		<?=showSpinBox('task_percent_complete', $obj->task_percent_complete, 0, 100, '%', 5)?>
		</td>
	</tr>
	<tr>
<?php
	if ($obj->canUserEditTimeInformation()) {
?>
		<td align='right'>
			<?php echo $AppUI->_("Due"); ?>
		</td>
		<td align="left" nowrap="nowrap">
			<?php
				$end_date = intval( $obj->task_end_date ) ? new CDate( $obj->task_end_date ) : null;
			?>
			<input id="idDateEnd" type="hidden" name="task_end_date" value="<?php echo $end_date ? $end_date->format( FMT_TIMESTAMP ) : '';?>" />
			<input id="idDateEndD" type="text" name="end_date" value="<?php echo $end_date ? $end_date->format( $df ) : '';?>" class="text" disabled="true" style="width: 64px"/>
			<img id="idDateEndB" src="images/calendar.gif" title="<?php echo $AppUI->_('Calendar');?>" class="ico"/>
		</td>
<?php
	}
?>
	<?php if ( $obj->task_owner != $AppUI->user_id ) { ?>
		<td align="right" colspan="2"><input id="idNotifyOwner" type="checkbox" name="task_log_notify_owner"/></td>
		<td align="left" colspan="2"><label for="idNotifyOwner"><?=$AppUI->_('Notify task creator')?></label></td>
	<? } ?>
	</tr>
	<tr>
		<td align="right"><?php echo $AppUI->_('Summary');?></td>
		<td align="left" colspan="100%">
			<input style="width: 95%" type="text" class="text" name="task_log_name" value="<?php echo $log->task_log_name;?>" maxlength="255" size="30" />
		</td>
	</tr>
	<tr>
		<td align="right" valign="top"><?php echo $AppUI->_('Description');?></td>
		<td align="left" colspan="100%">
			<textarea name="task_log_description" class="textarea" rows="3" style="width: 95%"><?php echo $log->task_log_description;?></textarea>
		</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td style="padding-left: 0px" colspan="100%" align="left">
			<table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td align="right">
						<input id="idLogProblem" type="checkbox" value="1" name="task_log_problem"
							<?=$log->task_log_problem?'checked="checked"':''?>>
					</td>
					<td align="left" nowrap="nowrap">
						<label for="idLogProblem"><?=$AppUI->_('Log as problem issue')?></label>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	</table>
</td>
<td width="60%" valign="top" align="left">
	<table width='100%'>
		<tr><td><?=$AppUI->_('Reference URL')?></td></tr>
		<tr>
		<td>
			<input type="text" class="text" name="task_log_related_url" style="width: 98%"
				value="<?=$log->task_log_related_url?>" maxlength="255"
				title="<?=$AppUI->_('Must in general be entered with protocol name, e.g. http://...')?>"/>
		</td>
		</tr>
		<tr>
		<td>
			<fieldset style="border: 1px solid #C0DAEA; padding: 2px">
				<legend>&nbsp;<?=$AppUI->_('Email Log to')?>&nbsp;</legend>
<?php
	$tl = $AppUI->getPref('TASKLOGEMAIL');
	$ta = $tl & 1;
	$tt = $tl & 2;
	$tp = $tl & 4;
?>
		<table border="0" cellpadding="0" cellspacing="0" width="100%" class="internal">
		<tr>
			<td align="right">
				<input id="idEmailA" type="checkbox" name="email_assignees" <?=$ta ? 'checked="checked"' : ''?>>
			</td>
			<td nowrap="nowrap"><label for="idEmailA"><?=$AppUI->_('Task Assignees')?></label>
				<input type="hidden" name="email_task_list" id="email_task_list"
					value="<?php
						$task_email_title = array();
						$q = new DBQuery;
						$q->addTable('task_contacts', 'tc');
						$q->leftJoin('contacts', 'c', 'c.contact_id = tc.contact_id');
						$q->addWhere("tc.task_id = '$task_id'");
						$q->addQuery('tc.contact_id');
						$q->addQuery('CONCAT(c.contact_first_name, " ", c.contact_last_name) AS contact_name');
						$req =& $q->exec();
						$cid = array();
						for ($req; ! $req->EOF; $req->MoveNext()) {
							$cid[] = $req->fields['contact_id'];
							$task_email_title[] = $req->fields['contact_name'];
						}
						echo implode(',', $cid);
						?>">
			</td>
			<td rowspan="4" nowrap="nowrap">&nbsp;&nbsp;</td>
			<td rowspan="4" width="100%" valign="top">
				<?php echo $AppUI->_('Others');?>
				<textarea class="text" name="email_extras" rows="3" maxlength="255" style="width: 97%"></textarea>
			</td>
		</tr>
		<tr>
			<td align="right">
				<input id="idEmailTC" type="checkbox" title="<?=addslashes(implode(',',$task_email_title))?>"
					name="email_task_contacts" id="email_task_contacts" <?=$tt ? 'checked="checked"' : ''?>>
			</td>
			<td nowrap="nowrap"><label for="idEmailTC"><?=$AppUI->_('Task Contacts')?></label>
				<input type="hidden" name="email_project_list" id="email_project_list"
					value="<?php
						$q->clear();
						$q->addTable('project_contacts', 'pc');
						$q->leftJoin('contacts', 'c', 'c.contact_id = pc.contact_id');
						$q->addWhere("pc.project_id = '$obj->task_project'");
						$q->addQuery('pc.contact_id');
						$q->addQuery('CONCAT(c.contact_first_name, " ", c.contact_last_name) AS contact_name');
						$req =& $q->exec();
						$cid = array();
						$proj_email_title = array();
						for ($req; ! $req->EOF; $req->MoveNext()) {
							if (! in_array($req->fields['contact_id'], $cid)) {
								$cid[] = $req->fields['contact_id'];
								$proj_email_title[] = $req->fields['contact_name'];
							}
						}
						echo implode(',', $cid);
						$q->clear();
					?>">
			</td>
		</tr>
		<tr>
			<td align="right">
				<input id="idEmailPC" type="checkbox" title="<?=addslashes(implode(',', $proj_email_title))?>"
					name="email_project_contacts" id="email_project_contacts" <?=$tp ? 'checked="checked"' : ''?>>
			</td>
			<td nowrap="nowrap">
				<label for="idEmailPC"><?php echo $AppUI->_('Project Contacts');?></label>
				<input type="hidden" name="email_others" id="email_others" value="">
			</td>
		</tr>
		<tr><td>&nbsp;</td></tr>
		</table>
		</fieldset>
		</td>
	</table>
</td>
</tr>
</table>
</form>
